import Vue from 'vue';
import Router from 'vue-router';
import layout  from '@/layout';
import authUtils from '../utils/authUtils'
import login  from '@/pages/login';
import error from '@/pages/error';
import home from '@/pages/home';
import mgmtRoutes from './mgmt-routes';
import infoRoutes from './info-routes';

Vue.use(Router);

var appRouter = new Router({ 
    routes:[
        {
            name: 'login',
            path: '/login',
            meta: { title: '登录' },
            component: login
        },
        {
            path: '/',
            redirect: '/login',
            hidden: true,
            name:'root'
        },
        {
            path: '/home',
            component: layout, meta: { title: '首页' },
            children: [
                { name:'Home', path: '', meta: { title: '首页', keepAlive:true }, component: home },
            ]
        },
        {
            path: '/sys', component: layout, meta: { title: '系统管理配置' },
            children: [
                ...mgmtRoutes,
                ...infoRoutes
            ]
        },        
        {
            path: '*', component: layout, meta: { title: '错误页面' },
            children: [
                { name:'error',path: '*', meta: { title: '错误页面' }, component: error },
            ]
        }    
    ]
});

appRouter.beforeResolve((to, from, next) => {
	
	//检查sessionStorage中是否有用户信息，有的话放行，没有就定向回登录页面
    //其实这样不合理，可以伪造，有待改善
    //console.log(to);
	if(!authUtils.isLogin()) {
        //
        if( to.path.indexOf('/login') == -1 && to.path != '/'){
            authUtils.logout(appRouter);
        }
    }
    next();
});

appRouter.beforeEach((to, from, next) => {

    next();
});

export default appRouter;